#include "bsp_aht21_driver.h"
#include "delay.h"
#define AHT_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE()

iic_interface_t*  g_iic_interface;
sys_tick_t*       g_sys_tick;
iic_bus_t*        g_iic_bus;

uint8_t  AHT_Read_Status(void)
{
    uint8_t Byte_first;	
	IICStart(g_iic_bus);
	IICSendByte(g_iic_bus,0x71);
	IICWaitAck(g_iic_bus);
	Byte_first = IICReceiveByte(g_iic_bus);
	IICSendNotAck(g_iic_bus);
	IICStop(g_iic_bus);	
	return Byte_first;
}

void  AHT_Reset(void)
{
    IICStart(g_iic_bus);
	IICSendByte(g_iic_bus,0x70);
	IICWaitAck(g_iic_bus);
	IICSendByte(g_iic_bus,0xBA);
	IICWaitAck(g_iic_bus);
	IICStop(g_iic_bus);
}
uint8_t  AHT_Init(void)
{
    AHT_CLK_ENABLE;
	IICInit(g_iic_bus);
	
	delay_ms(40);
	
	if(AHT_Read_Status()&&0x08!=0x08)
	{
		IICStart(g_iic_bus);
		IICSendByte(g_iic_bus,0x70);
		IICWaitAck(g_iic_bus);
		IICSendByte(g_iic_bus,0xBE);
		IICWaitAck(g_iic_bus);
		IICSendByte(g_iic_bus,0x08);
		IICWaitAck(g_iic_bus);
		IICSendByte(g_iic_bus,0x00);
		IICWaitAck(g_iic_bus);
		IICStop(g_iic_bus);
		delay_ms(10);
	}
	//AHT_Reset();
	
	return 0;
}
uint8_t AHT_Read(float *humi, float *temp)
{
    uint8_t cnt=5;
	uint8_t  Byte_1th=0;
	uint8_t  Byte_2th=0;
	uint8_t  Byte_3th=0;
	uint8_t  Byte_4th=0;
	uint8_t  Byte_5th=0;
	uint8_t  Byte_6th=0;
	uint32_t RetuData = 0;
	
	IICStart(g_iic_bus);
	IICSendByte(g_iic_bus,0x70);
	IICWaitAck(g_iic_bus);
	IICSendByte(g_iic_bus,0xAC);
	IICWaitAck(g_iic_bus);
	IICSendByte(g_iic_bus,0x33);
	IICWaitAck(g_iic_bus);
	IICSendByte(g_iic_bus,0x00);
	IICWaitAck(g_iic_bus);
	IICStop(g_iic_bus);	
	
	delay_ms(80);
	while(AHT_Read_Status()&0x80==0x80 && cnt)
	{
		delay_ms(5);
		cnt--;
		AHT_Read_Status();
	}
	if(!cnt)
	{return 1;}
	
	IICStart(g_iic_bus);
	IICSendByte(g_iic_bus,0x71);
	IICWaitAck(g_iic_bus);
	Byte_1th = IICReceiveByte(g_iic_bus);
	IICSendAck(g_iic_bus);
	Byte_2th = IICReceiveByte(g_iic_bus);
	IICSendAck(g_iic_bus);
	Byte_3th = IICReceiveByte(g_iic_bus);
	IICSendAck(g_iic_bus);
	Byte_4th = IICReceiveByte(g_iic_bus);
	IICSendAck(g_iic_bus);
	Byte_5th = IICReceiveByte(g_iic_bus);
	IICSendAck(g_iic_bus);
	Byte_6th = IICReceiveByte(g_iic_bus);
	IICSendNotAck(g_iic_bus);
	IICStop(g_iic_bus);

	RetuData = (RetuData|Byte_2th)<<8;
	RetuData = (RetuData|Byte_3th)<<8;
	RetuData = (RetuData|Byte_4th);
	RetuData =RetuData >>4;
	*humi = (RetuData * 1000 >> 20);
	*humi /= 10;
	
	RetuData = 0;
	RetuData = (RetuData|(Byte_4th&0x0f))<<8;
	RetuData = (RetuData|Byte_5th)<<8;
	RetuData = (RetuData|Byte_6th);
	RetuData = RetuData&0xfffff;
	*temp = ((RetuData * 2000 >> 20)- 500);
	*temp /= 10;
	
	return 0;
}



void  initStr(bsp_aht21_driver_t  *bsp_aht21_driver, 
                iic_interface_t     *iic_interface,
                sys_tick_t          *sys_tick,
                iic_bus_t           *iic_bus)
{
	//参数验证

	//给实体bsp_aht21_driver赋值


    //本层函数指针赋值
    bsp_aht21_driver->pfAHT_Read_Status = AHT_Read_Status;
    bsp_aht21_driver->pfAHT_Reset       = AHT_Reset;
    bsp_aht21_driver->pfAHT_Init        = AHT_Init;
    bsp_aht21_driver->pfAHT_Read        = AHT_Read;

	//调用aht_init


    //上层传递的接口
    g_iic_interface = iic_interface;
    g_sys_tick      = sys_tick;
    g_iic_bus       = iic_bus;

}
